package mobi.hami.huapin.service.impl;

import mobi.hami.huapin.core.stock.StockUtil;
import mobi.hami.huapin.dao.UserOrderDetailMapper;
import mobi.hami.huapin.dto.SKUPresentDTO;
import mobi.hami.huapin.model.UserOrderDetail;
import mobi.hami.huapin.service.UserOrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * Create by 小峰 on 2018/9/1.
 */
@Service
public class UserOrderDetailServiceImpl implements UserOrderDetailService {
    @Autowired
    private UserOrderDetailMapper userOrderDetailMapper;

    @Override
    public List<UserOrderDetail> getListByOrderId(Integer id) {
        return userOrderDetailMapper.getListbyOrderId(id);
    }

    /**
     * 包含所有
     *
     * @param id
     * @return
     */
    @Override
    public List<UserOrderDetail> getListByOrderIdAll(Integer id) {
        return userOrderDetailMapper.getListbyOrderIdAll(id);
    }

    @Override
    public int addList(List<UserOrderDetail> userOrderDetailList, Integer orderId) {
        return userOrderDetailMapper.addList(userOrderDetailList,orderId);
    }

    @Override
    public Integer saveUserOrderDetailByModel(UserOrderDetail userOrderDetail) {
        return userOrderDetailMapper.insertSelective(userOrderDetail);
    }

    /**
     * 增加赠品到订单信息中
     * @param skuPresent
     * @param orderId
     * @return
     */
    @Override
    public Integer instanceUserOrderDetail(SKUPresentDTO skuPresent, Integer orderId,String orderCode) {

        UserOrderDetail userOrderDetail = new UserOrderDetail();
        userOrderDetail.setOrder_id(orderId);
        userOrderDetail.setSku_id(skuPresent.getId());
        userOrderDetail.setSku_name(skuPresent.getSkuname());
        userOrderDetail.setAttribute_name(skuPresent.getAttribute_name());
        userOrderDetail.setSku_photo(skuPresent.getPictuire());
        userOrderDetail.setSku_money(new BigDecimal("0"));
        userOrderDetail.setSku_type(1);
        userOrderDetail.setSku_cout(1);
        userOrderDetail.setIs_valid(1);
        saveUserOrderDetailByModel(userOrderDetail);
        new StockUtil().addRepertoryBySkuInfo(orderCode,skuPresent.getId(),1);
        return userOrderDetail.getId();
    }
}
